package com.bins.uaa.auth.service;

import com.bins.uaa.auth.feign.UserClient;
import com.bins.uaa.auth.security.UserJwt;
import com.bins.uaa.auth.vo.SysUserVo;
import com.bins.uaa.user.api.vo.MenuVo;
import com.bins.uaa.user.api.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("userService")
public class CustomUserDetailsService implements UserDetailsService {

    @Autowired
    ClientDetailsService clientDetailsService;

    @Autowired
    UserClient userClient;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserVo userVo = new UserVo();
        userVo.setUsername("andybin");
        userVo.setPassword(new BCryptPasswordEncoder().encode("123456"));
        userVo.setPermissions(new ArrayList<MenuVo>());

//        String password ="123456";
//        List<MenuVo> permissions = userVo.getPermissions();
//        List<String> user_permission = new ArrayList<>();
//        permissions.forEach(item-> user_permission.add(item.getCode()));
//        String user_permission_string  = StringUtils.join(user_permission.toArray(), ",");
//        UserJwt userDetails = new UserJwt(username,
//                password,
//                AuthorityUtils.commaSeparatedStringToAuthorityList(user_permission_string));

        SysUserVo userDetails = new SysUserVo();
        userDetails.setId(userVo.getId());
        userDetails.setUsername(userVo.getUsername());
        userDetails.setPassword(userVo.getPassword());
        return userDetails;
    }
}
